﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TeacherHelp.FormList.Teacher
{
    public partial class ExercisesListForm : BanFlashingForm
    {
        private readonly TeacherMainForm _parentForm;

        private ExercisesListForm()
        {
            InitializeComponent();
        }
        public ExercisesListForm(TeacherMainForm teacherMainForm) : this()
        {
            _parentForm = teacherMainForm;
        }

        private void SerachBtn_Click(object sender, EventArgs e)
        {
            var subjectValue = SubjectsComBox.SelectedValue.ToInt();
            var subjectName = subjectValue == default ? null : subjectValue.ToString();
            var difficultyValue = ExercisesDifficultyComBox.SelectedValue.ToString();
            BLL.ExercisesManager exercisesManager = new BLL.ExercisesManager();
            var result = exercisesManager.GetExercisesAndSubject(subjectName, difficultyValue);
            this.ExercisesInfoGridView.DataSource = result;
        }

        private void ExercisesListForm_Load(object sender, EventArgs e)
        {
            BLL.SubjectsManager subjectsManager = new BLL.SubjectsManager();
            var subjectList = subjectsManager.GetSubejctAll();
            subjectList.Insert(0, new Model.Subjects { Name = "全部" });
            SubjectsComBox.DataSource = subjectList;
            SubjectsComBox.DisplayMember = nameof(Model.Subjects.Name);
            SubjectsComBox.ValueMember = nameof(Model.Subjects.Id);

            var difficultyList = new List<dynamic> {
                new { Id = "",Value = "全部"},
                new { Id = "1",Value = "简单"},
                new { Id = "2",Value = "中等"},
                new { Id = "3",Value = "困难"}
            };
            ExercisesDifficultyComBox.DataSource = difficultyList;
            ExercisesDifficultyComBox.DisplayMember = "Value";
            ExercisesDifficultyComBox.ValueMember = "Id";

            BLL.ExercisesManager exercisesManager = new BLL.ExercisesManager();
            this.ExercisesInfoGridView.DataSource = exercisesManager.GetExercisesAndSubject();
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _parentForm.ExercisesEdit();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要删除吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.No)
            {
                return;
            }
            var selectRow = this.ExercisesInfoGridView.SelectedRows;
            if (selectRow.Count == 0)
            {
                MessageBox.Show("请选择行！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            var exercisesId = selectRow[0].Cells[nameof(Model.Exercises.Id)].Value.ToString();
            BLL.ExercisesManager exercisesManager = new BLL.ExercisesManager();
            string result = exercisesManager.ExercisesRemove(exercisesId.ToInt());
            if (string.IsNullOrEmpty(result))
            {
                //var that = this;
                //Task.Run(() => that.ExercisesInfoGridView.DataSource = exercisesManager.GetExercisesAndSubject());

                this.ExercisesInfoGridView.DataSource = exercisesManager.GetExercisesAndSubject();
                MessageBox.Show("删除成功！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
